<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      vloading="formLoading"
      label-width="auto"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="编号" prop="fileNo">
            <el-input v-model="formData.fileNo" placeholder="请输入编号" :clearable="true" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="办公区" prop="areaId">
            <el-select v-model="formData.areaId" placeholder="请输入办公区">
              <el-option
                v-for="dict in areaList"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="权利人" prop="obligee">
            <el-input v-model="formData.obligee" placeholder="请输入权利人" :clearable="true" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="座落" prop="location">
            <el-input v-model="formData.location" placeholder="请输入座落" :clearable="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="不动产编号" prop="realEstateNo">
            <el-input
              v-model="formData.realEstateNo"
              placeholder="请输入不动产编号"
              :clearable="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="面积" prop="acreage">
            <InputNumber
              type="decimal"
              v-model="formData.acreage"
              placeholder="请输入面积"
              :clearable="true"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="formData.remark" placeholder="请输入备注" :clearable="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="附件" prop="fileUrl">
            <UploadFile v-model="formData.fileUrls" :isShowTip="false" :drag="true" :limit="1" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" :disabled="formLoading" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup name="RealEstateEdit" lang="ts">
import * as OfficeAreaApi from '@/api/area/officeArea'
import type { UploadUserFile } from 'element-plus'
import * as RealEstateApi from '@/api/files/realEstate'
import * as RealEstateModel from '@/model/files/certificate/FilesRealEstate'
const areaList = ref()
const message = useMessage()
let formType = ''
const { t } = useI18n()
const dialogTitle = ref('')
const dialogVisible = ref(false)
const formRef = ref()
const formLoading = ref(false)
const formData = ref({
  id: undefined,
  fileNo: '',
  areaId: undefined,
  obligee: '',
  location: '',
  realEstateNo: '',
  acreage: 0,
  remark: '',
  fileUrl: '',
  fileUrls: [] as UploadUserFile[],
  fileName: ''
})
const formRules = reactive({
  fileNo: [{ required: true, type: 'string', message: '请输入编号', trigger: 'blur' }],
  areaId: [{ required: true, type: 'number', message: '请选择办公区', trigger: 'blur' }]
})

watch(
  () => formData.value.fileUrls,
  (newFileUrls) => {
    if (newFileUrls != undefined && newFileUrls.length > 0 && newFileUrls[0]['url'] != undefined) {
      formData.value.fileUrl = newFileUrls[0]['url']
      formData.value.fileName = newFileUrls[0]['name']
    }
  }
)
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  areaList.value = await OfficeAreaApi.simpleList()
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await RealEstateApi.singleGet(id)
      if (formData.value.fileUrl != undefined && formData.value.fileUrl != '') {
        formData.value.fileUrls = [{ name: formData.value.fileName, url: formData.value.fileUrl }]
      } else {
        formData.value.fileUrls = []
      }
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    fileNo: '',
    areaId: undefined,
    obligee: '',
    location: '',
    realEstateNo: '',
    acreage: 0,
    remark: '',
    fileUrl: '',
    fileUrls: [] as UploadUserFile[],
    fileName: ''
  }

  formRef.value?.resetFields()
}

const emit = defineEmits(['success'])
/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return

  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as RealEstateModel.FilesRealEstate
    if (formType === 'create') {
      await RealEstateApi.create(data)
      message.success(t('common.createSuccess'))
    } else {
      await RealEstateApi.update(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>
